﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;

/// <summary>
/// Summary description for DataMaintWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class DataMaintWebService : System.Web.Services.WebService {


    static bool bWorkingDisconnected = Convert.ToBoolean(ConfigurationManager.AppSettings["WorkingDisconnected"]);

    public DataMaintWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }


    [WebMethod]
    static public string ResetCacheDate()
    {
        DateTime dtServerSales = DateTime.MinValue;
        DateTime dtTeradata = DateTime.MinValue;
        dtServerSales = Convert.ToDateTime(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\ServerSalesDate.txt")));
        DateTime dtCachedSales = Convert.ToDateTime(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\CachedSalesDate.txt")));
        System.Web.HttpContext.Current.Application["CacheDate"] = dtCachedSales;

        if (!bWorkingDisconnected)
        {

            bool bFromFile = dtServerSales == dtCachedSales;
            if (!bFromFile)
            {
                System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\CachedSalesDate.txt"), dtServerSales.ToString());
                System.Web.HttpContext.Current.Application["CacheDate"] = dtServerSales;
            }
        }

        HttpContext.Current.Application["Reload Cache"] = false;
        return "ResetCacheDate to " + dtTeradata.ToShortDateString();
    }

    [WebMethod]
     static   public string RebuildTheCache()
    {
        //We fully reload the cache with these datasets every time the app restarts since they are volatile
        Caching oACCCache = new Caching();
        oACCCache.Get_DataSet_SQLServer("SCORE_ACC_PROPERTIES", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "");
        oACCCache.Get_DataSet_From_SQLServer("ACC_DATA", 1, oACCCache.RefreshTheCache, false, "");

        ////This table has ~800k rows, so the initial load needs to pull it in segments, probably 3 months at a time
        //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-385 AND DATE-270");
        ////need to NOT TRUNCATE THE TABLE IN THESE TWO
        //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-269 AND DATE-150");
        //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-149 AND DATE-1"); 

        oACCCache.Get_DataSet_SQLServer("SCORE_MTD_SALES_ROLLUPS+SCORE_ACC_ORDERS_MTD+SCORE_ACC_ORDERS_PMTD+SCORE_ACC_ORDERS_PMON", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "");

        //Fully reloaded to SQL Server each restart

        //For these datasets, we only get the current month's data, i.e. an incremental load, since the prior months' totals don't change.
        //and they are so large, we don't cache them but select the data we need from SQL Server
        //Set the true to false below to load all the data. Also, change the SCORE_ACC_RAW_ORDERS query
        oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "BETWEEN DATE-EXTRACT(DAY from (DATE-1)) AND DATE-1");
        oACCCache.Get_DataSet_SQLServer("SCORE_STATS_YR_MN_DY+SCORE_ACC_RAW_ORDERS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");
        oACCCache.Get_DataSet_SQLServer("SCORE_STATS_YR_MN+SCORE_ACC_RAW_ORDERS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");
        //        oACCCache.Get_DataSet_SQLServer("ACC_CHANNEL_ROLLUPS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");


        //oACCCache.Get_DataSet("ACC_MARKET_PENETRATION+ACC_PENETRATION", 0, oACCCache.RefreshTheCache, true, "");
        //oACCCache.Get_DataSet("MTD_SALES_ROLLUPS", 1, oACCCache.RefreshTheCache, true, "");
        return "Rebuilt Cache";
    }

}
